package com.gm.wholesale.module.order.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.wholesale.R;
import com.gm.wholesale.cacheData.CacheData;
import com.gm.wholesale.module.order.bean.OrderRecmPriceBean;
import com.gm.wholesale.util.Utils;

public class OrderRightFunctionView extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private TextView toWeighTv;
    private TextView addWeighTv;
    private TextView priceOneTv;
    private TextView priceTwoTv;
    private TextView priceThreeTv;
    private CreateOrderView createOrderView;

    private OrderRecmPriceBean mOrderRecmPriceBean;

    private RelativeLayout priceRL1;

    private RelativeLayout priceRL2;

    private RelativeLayout priceRL3;

    public void setCreateOrderView(CreateOrderView createOrderView) {
        this.createOrderView = createOrderView;
    }

    public OrderRightFunctionView(Context context) {
        super(context);
        initView(context);
    }

    public OrderRightFunctionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_order_right_function_view, this);
        toWeighTv = findViewById(R.id.toWeighTv);
        addWeighTv = findViewById(R.id.addWeighTv);
        priceOneTv = findViewById(R.id.priceOneTv);
        priceTwoTv = findViewById(R.id.priceTwoTv);
        priceThreeTv = findViewById(R.id.priceThreeTv);
        toWeighTv.setOnClickListener(this);
        addWeighTv.setOnClickListener(this);
        priceOneTv.setOnClickListener(this);
        priceTwoTv.setOnClickListener(this);
        priceThreeTv.setOnClickListener(this);
        priceRL1 = findViewById(R.id.priceRL1);
        priceRL2 = findViewById(R.id.priceRL2);
        priceRL3 = findViewById(R.id.priceRL3);
    }

    public void hidPrice() {
        priceRL1.setVisibility(GONE);
        priceRL2.setVisibility(GONE);
        priceRL3.setVisibility(GONE);
    }

    public void showPrice() {
        priceRL1.setVisibility(VISIBLE);
        priceRL2.setVisibility(VISIBLE);
        priceRL3.setVisibility(VISIBLE);
    }

    public void hidWeightView() {
        toWeighTv.setVisibility(GONE);
        addWeighTv.setVisibility(GONE);
    }

    public void showWeightView() {
        toWeighTv.setVisibility(VISIBLE);
        addWeighTv.setVisibility(VISIBLE);
    }

    public void reset() {
        priceOneTv.setText("￥0");
        priceTwoTv.setText("￥0");
        priceThreeTv.setText("￥0");
        showWeightView();
        showPrice();
        setVisibility(VISIBLE);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.toWeighTv) {
            createOrderView.getWeightByMachine();
        }
        if (id == R.id.addWeighTv) {
            createOrderView.shoAddWeightView();
        }
        if (mOrderRecmPriceBean == null) {
            return;
        }
        if (id == R.id.priceOneTv) {
            createOrderView.setPrice(mOrderRecmPriceBean.getLastPrice());
        }
        if (id == R.id.priceTwoTv) {
            createOrderView.setPrice(mOrderRecmPriceBean.getHighFrequencyPrice());
        }
        if (id == R.id.priceThreeTv) {
            createOrderView.setPrice(mOrderRecmPriceBean.getTagPrice());
        }

    }

    public void updateView(OrderRecmPriceBean orderRecmPriceBean) {
        if (orderRecmPriceBean != null) {
            if (CacheData.getShowPriceView()){
                showPrice();
                mOrderRecmPriceBean = orderRecmPriceBean;
                priceOneTv.setText("￥" + orderRecmPriceBean.getLastPrice());
                priceTwoTv.setText("￥" + orderRecmPriceBean.getHighFrequencyPrice());
                priceThreeTv.setText("￥" + orderRecmPriceBean.getTagPrice());
            }
            else {
                hidPrice();
            }
            showGoodPrice(orderRecmPriceBean);
        }
    }

    private void showGoodPrice(OrderRecmPriceBean orderRecmPriceBean) {
        if (createOrderView == null) {
            return;
        }
        double price = Utils.getOrderRecmPrice(orderRecmPriceBean);
        createOrderView.autoSetPrice(price);


    }
}

